package com.atgouwu.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.atgouwu.mall.product.vo.CatelogTwoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atgouwu.mall.product.entity.CategoryEntity;
import com.atgouwu.mall.product.service.CategoryService;
import com.atgouwu.mall.common.utils.PageUtils;
import com.atgouwu.mall.common.utils.R;

/**
 * 商品三级分类
 *
 * @author heran
 * @email sunlightcs@gmail.com
 * @date 2024-02-22 15:17:48
 */
@Api(tags = "三级分类")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));
        return R.ok();
    }

//    //获取所有一级分类
//    @GetMapping(value = "/getLevelOneCategory")
//    @ApiOperation(value = "获取所有一级分类", notes = "返回一级分类对象集合")
//    private R getLevelOneCategory() {
//        List<CategoryEntity> categoryEntities = categoryService.getLevelOneCategory();
//        return R.ok().put("data", categoryEntities);
//    }
//
//    //获取三级分类{一级分类id:二三级分类对象}
//    @GetMapping(value = "/getCatalogJson")
//    @ApiOperation(value = "获取三级分类", notes = "k是一级分类id:v是二三级分类对象")
//    public R getCatalogJson() {
//        Map<String, List<CatelogTwoVo>> catalogJson = categoryService.getCatalogJson();
//        return R.ok().put("data", catalogJson);
//    }

    //获取三级分类列表
    @ApiOperation(value = "获取三级分类", notes = "首页获取三级分类列表")
    @GetMapping("/api/getCategoryList")
    public R getCategoryList() {
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }

}
